package com.chery.media.library.adapter

import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.PagerAdapter

/**
 * @author MoYe_PanDeng
 * @ClassName: FragmentAdapter
 * @Description: viewpager适配
 * @date: 2021/5/2
 */
class FragmentAdapter(fm: FragmentManager?, private var fragments: List<Fragment>?) :
    FragmentStatePagerAdapter(
        fm!!, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
    ) {
    override fun getItem(position: Int): Fragment {
        return fragments!![position]
    }

    override fun getCount(): Int {
        return if (fragments == null) 0 else fragments?.size?:0
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        return super.instantiateItem(container, position)
    }

    override fun getPageTitle(position: Int): CharSequence {
        return ""
    }

    override fun getItemPosition(`object`: Any): Int {
        return PagerAdapter.POSITION_NONE
    }

    fun setTitles(fragments: List<Fragment>?) {
        this.fragments = fragments
        notifyDataSetChanged()
    }
}